<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-content">
        <div class="login-header">
          <h2 class="login-title">水果商城后台管理</h2>
          <p class="login-subtitle">Fresh & Healthy</p>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              class="custom-input"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              class="custom-input"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              class="login-button"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { login } from '@/api/login'
import { useLoginStore } from '@/store/loginStore'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const loginStore = useLoginStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await login(loginForm)
        if (res.data && res.data.code === 200) {
          loginStore.setUserInfo(res.data.data)
          ElMessage.success('登录成功')
          router.push('/')
        } else {
          ElMessage.error(res.data?.message || '登录失败')
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: url('@/assets/image/bg.jpg') center/cover no-repeat;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15),
              0 0 30px rgba(132, 250, 176, 0.35),
              inset 0 0 20px rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  position: relative;
  z-index: 1;
  border: 1px solid rgba(255, 255, 255, 0.6);
  animation: borderGlow 3s ease-in-out infinite alternate;
}

@keyframes borderGlow {
  from {
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15),
                0 0 30px rgba(132, 250, 176, 0.35),
                inset 0 0 20px rgba(255, 255, 255, 0.4);
    border-color: rgba(255, 255, 255, 0.5);
  }
  to {
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2),
                0 0 40px rgba(132, 250, 176, 0.5),
                inset 0 0 30px rgba(255, 255, 255, 0.6);
    border-color: rgba(255, 255, 255, 0.8);
  }
}

.login-container::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 450px;
  height: 450px;
  background: radial-gradient(
    circle,
    rgba(132, 250, 176, 0.4) 0%,
    rgba(143, 211, 244, 0.2) 40%,
    transparent 70%
  );
  transform: translate(-50%, -50%);
  z-index: 0;
  pointer-events: none;
  border-radius: 50%;
  filter: blur(20px);
  animation: pulseGlow 6s ease-in-out infinite alternate;
}

@keyframes pulseGlow {
  0% {
    opacity: 0.7;
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    opacity: 0.9;
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    opacity: 0.7;
    transform: translate(-50%, -50%) scale(1);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  color: rgba(44, 62, 80, 0.9);
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-subtitle {
  color: rgba(52, 73, 94, 0.9);
  font-size: 16px;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin: 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.custom-input :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  box-shadow: 0 0 15px rgba(132, 250, 176, 0.1);
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: rgba(132, 250, 176, 0.8);
  background: rgba(255, 255, 255, 0.75);
  box-shadow: 0 0 20px rgba(132, 250, 176, 0.3);
}

.custom-input :deep(.el-input__prefix) {
  color: #67e8b7;
  font-size: 18px;
  margin-right: 5px;
  transition: all 0.3s ease;
}

.custom-input:hover :deep(.el-input__prefix) {
  color: #2ad699;
  transform: scale(1.1);
}

.custom-input :deep(.el-input__inner::placeholder) {
  color: rgba(44, 62, 80, 0.6);
  font-weight: 500;
  letter-spacing: 0.5px;
  transition: all 0.3s ease;
}

.custom-input:hover :deep(.el-input__inner::placeholder),
.custom-input :deep(.el-input__wrapper.is-focus .el-input__inner::placeholder) {
  color: rgba(44, 62, 80, 0.8);
  letter-spacing: 0.8px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 2px;
  border-radius: 8px;
  background: linear-gradient(120deg, #67e8b7 0%, #5dcdff 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, 
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.3) 50%,
    rgba(255, 255, 255, 0) 100%
  );
  transition: all 0.6s cubic-bezier(0.22, 1, 0.36, 1);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 20px rgba(103, 232, 183, 0.3);
  background: linear-gradient(120deg, #5adfaa 0%, #4bc4ff 100%);
}

.login-button:hover::before {
  left: 100%;
}

.login-button:active {
  transform: translateY(1px);
  box-shadow: 0 3px 10px rgba(103, 232, 183, 0.2);
}

:deep(.el-message) {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

:deep(.el-message--success) {
  background: linear-gradient(120deg, rgba(132, 250, 176, 0.95) 0%, rgba(143, 211, 244, 0.95) 100%);
  border: none;
}

:deep(.el-message--error) {
  background: rgba(245, 108, 108, 0.95);
  border: none;
}
</style> 